/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.math.series;

import org.math.calculas.IntegerFunction;
import org.math.calculas.IntegerVariable;
import org.math.data.DataValue;

/**
 * This class encapsulates computing a series. It checks for convergence
 *  and divergence and the iterations used. it does not moel oscillations.
 * @author stewari1
 */
public class Series {

    /** Field description */
    private int limitN = 500;

    /** Field description */
    private boolean converged = false;

    /** Field description */
    private DataValue seriesSum = null;

    /** Field description */
    private double epsilon = 1.0E-4;

    /** Field description */
    private int iterationUsed = 0;

    /** Field description */
    private IntegerVariable integerVariable = null;

    /** Field description */
    private final IntegerFunction integerFunction;

    /**
     * Constructs ...
     *
     * @param integerFunction description
     */
    public Series(IntegerFunction integerFunction) {
        this.integerFunction = integerFunction;
    }

    /**
     * Constructs ...
     *
     * @param integerFunction description
     * @param epsilon description
     */
    public Series(IntegerFunction integerFunction, double epsilon) {

        this.integerFunction = integerFunction;
        this.epsilon         = epsilon;
    }

    /**
     * Constructs ...
     *
     * @param integerFunction description
     * @param epsilon description
     * @param limitN description
     */
    public Series(IntegerFunction integerFunction, double epsilon, int limitN) {

        this.integerFunction = integerFunction;
        this.epsilon         = epsilon;
        this.limitN          = limitN;
    }

    /**
     * a convenience method for an integer function that has more than one
     * integer variable and another of its variable has been committed for the
     * <code>IntegerFunction</code>. If this value is not set, it tries to use
     * the default integer variable of the integer function.
     * @param integerVariable
     */
    public void setIntegerVariable(IntegerVariable integerVariable) {
        this.integerVariable = integerVariable;
    }

    public DataValue evaluate() {

        if (integerVariable == null) {
            integerVariable = integerFunction.getIntegerVariable();
        }

        integerVariable.setIntegerValue(1);

        DataValue oldSum = integerFunction.eval();
        DataValue newSum = null;

        for (int i = 2; i < limitN; i++) {

            iterationUsed++;

            integerVariable.setIntegerValue(i);

            newSum = oldSum.plus(integerFunction.eval());

            if (newSum.minus(oldSum).abs().isLessThan(epsilon)) {

                this.converged = true;

                break;
            }

            oldSum = newSum.copy();
        }

        this.seriesSum = newSum;

        return newSum;
    }

    public int getIterationUsed() {
        return iterationUsed;
    }

    public double getEpsilon() {
        return epsilon;
    }

    public void setEpsilon(double epsilon) {
        this.epsilon = epsilon;
    }

    public boolean isConverged() {
        return converged;
    }

    public void setLimitN(int limitN) {
        this.limitN = limitN;
    }
}
